/**
 * 跟进提醒 API
 */
import request from '../request'

/**
 * 提醒类型
 */
export interface Reminder {
  id: number
  lead_id: number
  visitor_sheet_id?: number
  reminder_type: 'auto' | 'manual'
  trigger_reason?: string
  reminder_time: string
  assigned_to: number
  notes?: string
  status: 'pending' | 'sent' | 'completed' | 'cancelled'
  sent_at?: string
  completed_at?: string
  created_at: string
  updated_at: string
}

/**
 * 提醒详情（包含客户信息）
 */
export interface ReminderWithLead extends Reminder {
  lead_name: string
  lead_phone: string
  assignee_name: string
  assignee_email: string
}

/**
 * 创建提醒请求
 */
export interface ReminderCreate {
  lead_id: number
  visitor_sheet_id?: number
  reminder_type: 'auto' | 'manual'
  trigger_reason?: string
  reminder_time: string
  assigned_to: number
  notes?: string
}

/**
 * 更新提醒请求
 */
export interface ReminderUpdate {
  reminder_time?: string
  assigned_to?: number
  notes?: string
  status?: 'pending' | 'sent' | 'completed' | 'cancelled'
}

/**
 * 提醒列表响应
 */
export interface ReminderListResponse {
  total: number
  items: ReminderWithLead[]
  page: number
  page_size: number
}

/**
 * 今日待办响应
 */
export interface TodayTasksResponse {
  pending_count: number
  overdue_count: number
  tasks: ReminderWithLead[]
}

/**
 * 跟进提醒 API
 */
export const reminderApi = {
  /**
   * 获取今日待办
   */
  getTodayTasks(): Promise<{ data: TodayTasksResponse }> {
    return request.get('/v1/reminders/today-tasks')
  },

  /**
   * 获取提醒列表
   */
  getList(params?: {
    status?: string
    page?: number
    page_size?: number
  }): Promise<{ data: ReminderListResponse }> {
    return request.get('/v1/reminders/', { params })
  },

  /**
   * 创建提醒
   */
  create(data: ReminderCreate): Promise<{ data: Reminder }> {
    return request.post('/v1/reminders/', data)
  },

  /**
   * 更新提醒
   */
  update(reminderId: number, data: ReminderUpdate): Promise<{ data: Reminder }> {
    return request.put(`/v1/reminders/${reminderId}`, data)
  },

  /**
   * 标记完成
   */
  markComplete(reminderId: number): Promise<{ data: Reminder }> {
    return request.post(`/v1/reminders/${reminderId}/complete`)
  },

  /**
   * 删除提醒
   */
  delete(reminderId: number): Promise<void> {
    return request.delete(`/v1/reminders/${reminderId}`)
  }
}
